<?php
/**
 * Created by PhpStorm.
 * User: Egg
 * Date: 2018/3/8
 * Time: 13:27
 */

namespace Common\Common;


class HttpUtil
{
    /***
     * get请求
     * @param $url          请求地址
     * @return mixed
     */
    public static function httpGet($url)
    {
        //1.初始化
        $curl = curl_init();
        //配置curl
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //执行curl
        $res = curl_exec($curl);
        //关闭curl
        curl_close($curl);
        return $res;
    }

    /**
     * post请求
     * @param $url          请求地址
     * @param null $data 请求数据(json)
     * @return mixed
     *curl请求接口的post方法
     */
    static function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * post请求
     * @param $url          请求地址
     * @param $path         文件地址
     * @return mixed curl请求接口的post方法
     * curl请求接口的post方法，提交文件
     */
    static function https_file_request($url, $path)
    {
        $curl = curl_init();
        $data = array(
            'media' => $path,
        );
//        array("media"=>"@".dirname(__FILE__)."\w.jpg");
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($path)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}